//JavaScript代码区域
layui.use(['element', 'carousel', 'iconPicker', 'form','layer'], function() {

	const element = layui.element;
	const carousel = layui.carousel;
	const $ = jQuery = layui.jquery;

	const layer = layui.layer;

    //验证当前用户是否登录
    var _ticket = $.cookie("token");
    if(_ticket != null || _ticket != ""){
        $.ajax({
            url : "http://localhost:8083/user/token/" + _ticket,//单点登录链接
            dataType : "jsonp",
            type : "GET",
            success : function(data){
                if (data.code == 200) {
                    window.username = data.data.user.loginName;
                    $("#cur-user").html(username);
                } else {
                    layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
                    window.location.href = "http://localhost:8083/login";
                }
            }
        });
    } else {
        layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
        window.location.href = "http://localhost:8083/login";
    }

	//图标选择器
	var iconPicker = layui.iconPicker,
		form = layui.form;

	const tab = {
		tabAdd(filter, title, content, id) {
			element.tabAdd(filter, {
				title,
				content: content || '#', //支持传入html
				id
			});
			element.render('tab');
			location.hash = id;
		},
		tabDelete(filter, id) {
			element.tabDelete(filter, id); //删除 lay-id="xxx" 的这一项  
		},
		tabChange(filter, id) {
			element.tabChange(filter, id); //切换到 lay-id="yyy" 的这一项
			location.hash = id;
		}
	};

	/**
	 * 点击左栏nav切换右栏tab
	 */
	/*element.on('nav(home)', function(elem) {
		let judge = true;
		if(elem.attr('lay-href')) {

			$("#main-tab li").each(function(index, item) {

				item = $(item)

				if(elem.attr('lay-href') == item.attr('lay-id')) {
					//tab栏有，则直接切换即可
					tab.tabChange('layadmin-layout-tabs', elem.attr('lay-href'))
					judge = false;
					return false;
				}
			})

			if(judge) {
				//tab栏没有，则需要添加新的tab项
				/!*if(elem.attr('lay-href') == '/component/icon/') {

					tab.tabAdd('layadmin-layout-tabs', elem.text(), ``, elem.attr('lay-href'));
					location.hash = elem.attr('lay-href');
					tab.tabChange('layadmin-layout-tabs', elem.attr('lay-href'));

					
				}*!/


				/!*	tab.tabAdd('layadmin-layout-tabs', elem.text(), `<iframe frameborder='0' src="${elem.attr('data-name')}.html" onload="reinitIframe()" width="100%" height="100%"></iframe>`, elem.attr('lay-href'));
					location.hash = elem.attr('lay-href');
					tab.tabChange('layadmin-layout-tabs', elem.attr('lay-href'));*!/
					tab.tabAdd('layadmin-layout-tabs', elem.text(), '<div class="body-content"></div>', elem.attr('lay-href'));
					tab.tabChange('layadmin-layout-tabs', elem.attr('lay-href'));
					
					$(".body-content:last").load(`${elem.parent().attr('data-name')}.html`);
					
			}
		}

	});*/

	/**
	 * 点击右栏tab切换左栏nav
	 */
	//nav中item
	/*let navItem = Array.from($("#main-nav a")).filter(function(item) {
		return $(item).attr('lay-href')
	});*/

	element.on('tab(layadmin-layout-tabs)', function(obj) {

		let layid = $(this).attr('lay-id');

		let navA = navItem.find(function(item) {

			return $(item).attr('lay-href') === layid
		})
		location.hash = layid;
		$("#main-nav dd").removeClass('layui-this');
		$(navA).parents('dd').addClass('layui-this');

		$("#main-nav li.layui-nav-item").removeClass('layui-nav-itemed');
		$(navA).parents('li.layui-nav-item').addClass('layui-nav-itemed');

	});

	/**
	 * 首页carousel配置
	 */
	let el = $("#main-content .layadmin-carousel").get();
	el.forEach(item => {
		carousel.render({
			elem: item,
			width: '100%', //设置容器宽度
			height: '100%',
			autoplay: false,
			arrow: 'none', //始终显示箭头
			trigger: 'mouseenter'
		});
	});

	//use()方法右括号
	/*-----------------------------------------------------*/
	//旁栏收缩效果
	let LeftStyle = "position:absolute;right:0;z-index:998;overflow:hidden;";
	let RightStyle = "position:absolute;right:0;z-index:998;overflow:hidden;left:44px;";
	$('.layui-layout-left').children('li').eq(0).on('click',function(){
		if($(this).find('i').hasClass('layui-icon-shrink-right') == true){
			$(this).find('i').removeClass('layui-icon-shrink-right').addClass('layui-icon-spread-left');
			// 旁栏为收缩状态
            $("#main-nav .layui-nav-item a span").hide();
			$('.layui-bg-black').animate({"width": 60}, 300);
			$('.layui-right').attr("style",LeftStyle);
			$('.layui-right').animate({"width":$('.layui-right').width()+156, "left":60}, 300);
			$('.layui-logo').text("").css({"background":"##1389b4 url(./static/img/logo.png) no-repeat center "});
			$('.layui-logo').animate({"width":60}, 300);
			$('.layui-layout-left').animate({"left":60}, 300);
			$('.layui-layout-admin .layui-footer').animate({"left":60}, 300);
            $(".layui-nav-tree").css("width",67);
            //如果有打开的标签就关闭
            $("#main-nav li").removeClass('layui-nav-itemed');
		}
		else{
			$(this).find('i').removeClass('layui-icon-spread-left').addClass('layui-icon-shrink-right');
			// 旁栏为展开状态
            $("#main-nav .layui-nav-item a span").show();
			$('.layui-bg-black').animate({"width": 200}, 300);
			$('.layui-right').attr("style",RightStyle);
			$('.layui-right').animate({"width":$('.layui-right').width()-156, "left":200}, 300);
			$('.layui-logo').animate({"width":200}, 300, function(){
			    $('.layui-logo').removeAttr('style').attr("style","background-color:##1389b4;").text("智慧农业服务");
			});
			$('.layui-layout-left').animate({"left":200}, 300);
            $(".layui-nav-tree").css("width",200);
		}

	});
    $("#main-nav li").click(function () {
        if (!$("#LAY_app_flexible").hasClass('layui-icon-shrink-right')) {
            $("#LAY_app_flexible").removeClass('layui-icon-spread-left').addClass('layui-icon-shrink-right');
            // 旁栏为展开状态
            $("#main-nav .layui-nav-item a span").show();
            $('.layui-bg-black').animate({"width": 200}, 300);
            $('.layui-right').attr("style",RightStyle);
            $('.layui-right').animate({"width":$('.layui-right').width()-156, "left":200}, 300);
            $('.layui-logo').animate({"width":200}, 300, function(){
                $('.layui-logo').removeAttr('style').attr("style","background-color:##1389b4;").text("智慧农业云服务");
            });
            $('.layui-layout-left').animate({"left":200}, 300);
            $(".layui-nav-tree").css("width",200);
        }
    });

    //左侧菜单提示信息
    $("#main-nav li a").hover(function () {
        if (!$("#LAY_app_flexible").hasClass('layui-icon-shrink-right')) {
            layer.tips($(this).find("span").html(), this);
        }
    });

    //----------------------------------------
	//关闭标签	
	let layerContent = `<dl class="SelectDown">
							<dd layadmin-event="closeThisTabs" class="closeThisTabs">
								<a href="javascript:;" style="display:block;margin-left:25%;font-size:16px;">关闭当前标签页</a>
							</dd>
							<dd layadmin-event="closeOtherTabs" class="closeOtherTabs">
								<a href="javascript:;" style="display:block;margin-left:25%;font-size:16px;">关闭其它标签页</a>
							</dd>
							<dd layadmin-event="closeAllTabs" class="closeAllTabs">
								<a href="javascript:;" style="display:block;margin-left:25%;font-size:16px;">关闭全部标签页</a>
							</dd>
						</dl>`;
	$('.layui-tabs').on('mouseenter',function(){
		let StyleLeft = $("body").width()-284+'px';
		window.myindex = layer.open({
  			type: 0,
  			/*btn:['关闭'],*/
  			move:false,
  			shade:0,
  			offset:['104px',StyleLeft],
  			content: layerContent,
  			success:function(){
  					$('.SelectDown').children('dd').children('a').on('mouseenter ',function(){
  						$(this).parent('dd').css('background-color','#f8f8f8').siblings().css('background-color','white');
					}).on('mouseleave',function(){
						$(this).parent('dd').css('background-color','white');
					});
					$('.SelectDown').on('mouseleave',function(){
						layer.closeAll();
					});
					// 关闭当前标签页
					$('.closeThisTabs').on('click',function(){
						let prevDom = $('.layui-tab-title').children('li.layui-this').prev();
						let layId = $('.layui-tab-title').children('li.layui-this').prev().attr('lay-id');

						if($('.layui-tab-title').children('li').first().siblings('li').hasClass('layui-this')==true){
							$('.layui-tab-title').children('li').first().siblings('li.layui-this').detach();
							prevDom.addClass('layui-this');
							//---------------------------------------
							$("#main-nav a").each(function(index,element){
								if($(this).attr('lay-href')==layId){
									$(this).parent('dd').addClass('layui-this');
									$(this).parents('li.layui-nav-item').addClass('layui-nav-itemed').siblings('li.layui-nav-item').removeClass('layui-nav-itemed');
									tab.tabChange('layadmin-layout-tabs', $(this).attr('lay-href'));
								}
							});

						}
					});
					// 关闭其他标签
					$('.closeOtherTabs').on('click',function(){
						if($('.layui-tab-title').children('li').first().siblings('li').hasClass('layui-this')==true){
							let Storage = $('.layui-tab-title').children('li').first().siblings('li.layui-this');
							$('.layui-tab-title').children('li').first().siblings('li').detach();
							$('.layui-tab-title').append(Storage);
						}
					});
					// 关闭全部标签
					$('.closeAllTabs').on('click',function(){
						$('.layui-tab-title').children('li').first().siblings('li').detach();
						$('.layui-tab-title').children('li').first().addClass('layui-this');
						$('.layui-tab-title').children('li').first().addClass('layui-nav-itemed').siblings('li.layui-nav-item').removeClass('layui-nav-itemed');
						tab.tabChange('layadmin-layout-tabs', $('.layui-nav-tree').children('li').first().find('a').eq(1).attr('lay-href'));
					});
  				}
			});
	});
    /*$('.layui-tabs').on('mouseleave',function(){
        layer.close(myindex);
    });*/
	
	$('#LAY_app_refresh').on('click',function(){
		window.location = window.location.href;
	});
	//---------------------------------------------------------------------	
	//主题修改
	let shadeModel = `
		<div class="layui-layer-shade" id="layui-layer-shade" times="2" style="z-index: 19891015; background-color: rgb(0, 0, 0); opacity: 0.1;"></div>
	`;
	let colorTheme = `
		<form class="layui-form" action="" id="colorTheme">
		  <div class="layui-form-item">
		    <label class="layui-form-label">颜色选择</label>
		    <div class="layui-input-block">
		      <select name="city" lay-verify="required"  lay-filter="colorThemeChange">
		        <option value="Selected" selected>默认</option>
		        <option value="green">绿色</option>
		        <option value="blue">蓝色</option>
		        <option value="red">红色</option>
		      </select>
		    </div>
		  </div>
		</form>
	`;
		$('#layui-theme').on('click',function(){
		$('body').append(shadeModel);
		layer.open({
			type: 0, 
  			move:false,
  			shade:0,
  			offset:'t',
  			area:['500px','350px'],
  			content: colorTheme,
  			yes:function(index,layero){
  				$('#layui-layer-shade').detach();
  				layer.close(index);
  				if(localStorage.colorKind != 'background-color:#4E5465 !important;color:white;'&& localStorage.colorKind != undefined){
					$('.layui-logo').removeAttr('style').attr('style',localStorage.getItem("colorKind"));
					$('.layui-nav-tree').children('li.layui-nav-itemed').children('dl').children('dd.layui-this').children('a').attr('style',localStorage.getItem("colorKind"));	
  				}
  			},
  			cancel:function(){
  				$('#layui-layer-shade').detach();
  			},
  			success:function(){
  				form.render('select');
				// 进行颜色验证
				form.on('select(colorThemeChange)', function(data){
				  //复选框value值，也可以通过data.elem.value得到data.value 20222A
				  switch (data.value){
				  	case "green":
				  			localStorage.colorKind = 'background-color:#2F9688 !important;color:white;';
				  			break;
				  	case "blue":
				  			localStorage.colorKind = 'background-color:#1E9FFF !important;color:white;';
				  			break;
				  	case "red":
				  			localStorage.colorKind = 'background-color:#AA3130 !important;color:white;';
				  			break;
				  	default:
				  			localStorage.colorKind = 'background-color:#4E5465 !important;color:white;';
				  			break;		
				  }
				  if(localStorage.colorKind == 'background-color:#4E5465 !important;color:white;'){
				  	$('.layui-logo').removeAttr('style').attr('style','background-color:#393d49 !important;');
					$('.layui-nav-tree').children('li.layui-nav-itemed').children('dl').children('dd.layui-this').children('a').removeAttr('style').attr('style','background-color:#009688 !important;');
				  }

				});
  			},
  			end:function(){
  				if(localStorage.colorKind != 'background-color:#4E5465 !important;color:white;' & localStorage.colorKind != undefined ){
	  				$('.layui-logo').removeAttr('style').attr('style',localStorage.getItem("colorKind"));
					$('.layui-nav-tree').children('li.layui-nav-itemed').children('dl').children('dd.layui-this').children('a').attr('style',localStorage.getItem("colorKind"));
				}		
  			}
		});
		
		$('#layui-layer-shade').on('click',function(){
			$(this).detach();
			layer.closeAll('dialog');
		});
	});
	
	if(localStorage.colorKind != 'background-color:#4E5465 !important;color:white;' & localStorage.colorKind != undefined ){
		$('.layui-logo').removeAttr('style').attr('style',localStorage.getItem("colorKind"));
		$('.layui-nav-tree').children('li.layui-nav-itemed').children('dl').children('dd.layui-this').children('a').attr('style',localStorage.getItem("colorKind"));
	}	
	

	
	
});



/*
 * 函数定义
 */
function reinitIframe() {
	try {
		var bHeight = iframe.contentWindow.document.body.scrollHeight;
		var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
		var height = Math.max(bHeight, dHeight);
		$("iframe").height(height);
	} catch(ex) {}
}




